<HTML>
<HEAD>
<TITLE>JavaScript Source Code 3000:  Equivalents:  Base Converter</TITLE>
<META HTTP-EQUIV="JavaScript Source Code 3000" CONTENT = "no-cache">
<META NAME="date" CONTENT="2000-09-09">
<META NAME="channel" CONTENT="Web Developer">
<META NAME="author" CONTENT="Mike Maloney">
<META NAME="section" CONTENT="Equivalents">
<META NAME="description" CONTENT="Converts input between binary, ternary, quintal, octal, decimal, and hexadecimal bases.  Try entering a string of zeroes and ones in the binary field - when you click off, the script displays that number of the other bases.">

<SCRIPT LANGUAGE="JavaScript">
<!-- Original:  Mike Maloney -->
<!-- Web Site:  http://nfg.natfuel.com -->




<!-- Begin
var hex = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
function CKparseInt(n, r) {
for (var i = 0; i < n.length; ++i)
if (n.charAt(i) >= r) {
alert("Invalid digit");
return 0;
}
if (isNaN(M = parseInt(n, r)))
alert ("Invalid number");
return M;
}
// decimal to any other base 2 to 16
function DecimaltoAnother(A, radix) {
s = "";
while (A >= radix) {
s += hex[A % radix];  // remainder
A = Math.floor(A / radix); // quotient, rounded down
}
return transpose(s += hex[A]);
}
// string reversal
function transpose(s) {
N = s.length;
for (i = 0,t = ""; i < N; i++)
t += s.substring(N-i-1, N-i); // s.substring(from, to)
return t;
}
// convert item.value using radix
function EvalAny(item, r) {
M = CKparseInt(item.value, r); // check this one
for (var i = 0, MyForm = document.forms[0]; i < MyForm.length; ++i) // re-evaluate all
MyForm.elements[i].value = DecimaltoAnother(M, MyForm.elements[i].name.substr(1,3));
}
//  End -->
</script>
</HEAD>

<BODY BGCOLOR=#ffffff vlink=#0000ff>

<BR>
<center>
<table width=600 cellpadding=0 cellspacing=10>
<tr>
<td width=468 align=center>

    
    
</td>
<td width=120 align=center>
    
</td>
</tr>
</table>
<BR>
<BR>
<basefont size=3>
<FONT SIZE="+2" FACE="Helvetica,Arial">
<A HREF="../index.htm" /" TARGET="_top"><FONT COLOR="#0000FF"><b>Home</b></font></A>
<img src="../img/arrow.gif" /img/arrow.gif" height=13 width=7 border=0 alt="}">
<A HREF="index.htm" /equivalents/"><font color="#FF0000"><b>Equivalents</b></font></A>
<img src="../img/arrow.gif" /img/arrow.gif" height=13 width=7 border=0 alt="}">
<FONT COLOR="#006666"><b>Base Converter</b></font></font>
<BR>
<BR>
<table BORDER=0 WIDTH=486 CELLPADDING=3 CELLSPACING=0>
<tr><td><font FACE="helvetica,arial,geneva">
<br>
<br>
<!-- Description --><!--content_start-->
Converts input between binary, ternary, quintal, octal, decimal, and hexadecimal bases.  Try entering a string of zeroes and ones in the binary field - when you click off, the script displays that number of the other bases.
<hr>
</td></tr>
</table>
<!-- Demonstration -->
<center>
<h3>Base Conversion Function</h3><br>
<form method="post">
<table border=0 align=center>
<tr>
<td align=right><p>Binary:</p></td>
<td><input name="b002" value="0" onChange="EvalAny(this, 2)" size=27></input></td>
</tr>
<tr>
<td align=right>Ternary:</td>
<td><input name="t003" value="0" onChange="EvalAny(this, 3)" size=21></input></td>
</tr>
<tr><td align=right>Quintal:</td>
<td><input name="q005" value="0" onChange="EvalAny(this, 5)" size=16></input></td>
</tr>
<tr>
<td align=right>Octal:</td>
<td><input name="o008" value="0" onChange="EvalAny(this, 8)" size=12></input></td>
</tr>
<tr>
<td align=right>Decimal:</td>
<td><input name="d010" value="0" onChange="EvalAny(this, 10)" size=11></input></td>
</tr>
<tr>
<td align=right>Hexadecimal:</td>
<td><input name="h016" value="0" onChange="EvalAny(this, 16)" size=8></input></td>
</tr>
</table>
</form>
</center>
<P>
<P>
<a name="source">
<table BORDER=0 WIDTH=486 CELLPADDING=3 CELLSPACING=0>
<tr><td BGCOLOR=yellow><font FACE="helvetica,arial,geneva"><b>JavaScript Source Code 3000:  Equivalents:  Base Converter</b>
<p>Simply click inside the window below, use your cursor to highlight the script, and copy (type Control-c or Apple-c) the script into a new file in your text editor (such as Note Pad or Simple Text) and save (Control-s or Apple-s).  The script is yours!!!
<br><br></font></td></tr>
<tr><td BGCOLOR=yellow ALIGN=CENTER>
<form NAME="copy">

<DIV align="center">
<input type=button value="Highlight All" onClick="javascript:this.form.txt.focus();this.form.txt.select();">&nbsp;&nbsp;&nbsp;&nbsp;
<INPUT TYPE="text" NAME="total" VALUE="Script Size:  2.50 KB" size=24>
</DIV>

<textarea NAME="txt" ROWS=20 COLS=75 WRAP=VIRTUAL>

&lt;!-- TWO STEPS TO INSTALL BASE CONVERTER:

  1.  Copy the coding into the HEAD of your HTML document
  2.  Add the last code into the BODY of your HTML document  --&gt;

&lt;!-- STEP ONE: Paste this code into the HEAD of your HTML document  --&gt;

&lt;HEAD&gt;

&lt;SCRIPT LANGUAGE="JavaScript"&gt;
&lt;!-- Original:  Mike Maloney --&gt;
&lt;!-- Web Site:  http://nfg.natfuel.com --&gt;

&lt;! &gt;
&lt;! &gt;

&lt;!-- Begin
var hex = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
function CKparseInt(n, r) {
for (var i = 0; i &lt; n.length; ++i)
if (n.charAt(i) &gt;= r) {
alert("Invalid digit");
return 0;
}
if (isNaN(M = parseInt(n, r)))
alert ("Invalid number");
return M;
}
// decimal to any other base 2 to 16
function DecimaltoAnother(A, radix) {
s = "";
while (A &gt;= radix) {
s += hex[A % radix];  // remainder
A = Math.floor(A / radix); // quotient, rounded down
}
return transpose(s += hex[A]);
}
// string reversal
function transpose(s) {
N = s.length;
for (i = 0,t = ""; i &lt; N; i++)
t += s.substring(N-i-1, N-i); // s.substring(from, to)
return t;
}
// convert item.value using radix
function EvalAny(item, r) {
M = CKparseInt(item.value, r); // check this one
for (var i = 0, MyForm = document.forms[0]; i &lt; MyForm.length; ++i) // re-evaluate all
MyForm.elements[i].value = DecimaltoAnother(M, MyForm.elements[i].name.substr(1,3));
}
//  End --&gt;
&lt;/script&gt;
&lt;/HEAD&gt;

&lt;!-- STEP TWO: Copy this code into the BODY of your HTML document  --&gt;

&lt;BODY&gt;

&lt;center&gt;
&lt;h3&gt;Base Conversion Function&lt;/h3&gt;&lt;br&gt;
&lt;form method="post"&gt;
&lt;table border=0 align=center&gt;
&lt;tr&gt;
&lt;td align=right&gt;&lt;p&gt;Binary:&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;input name="b002" value="0" onChange="EvalAny(this, 2)" size=27&gt;&lt;/input&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=right&gt;Ternary:&lt;/td&gt;
&lt;td&gt;&lt;input name="t003" value="0" onChange="EvalAny(this, 3)" size=21&gt;&lt;/input&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td align=right&gt;Quintal:&lt;/td&gt;
&lt;td&gt;&lt;input name="q005" value="0" onChange="EvalAny(this, 5)" size=16&gt;&lt;/input&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=right&gt;Octal:&lt;/td&gt;
&lt;td&gt;&lt;input name="o008" value="0" onChange="EvalAny(this, 8)" size=12&gt;&lt;/input&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=right&gt;Decimal:&lt;/td&gt;
&lt;td&gt;&lt;input name="d010" value="0" onChange="EvalAny(this, 10)" size=11&gt;&lt;/input&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td align=right&gt;Hexadecimal:&lt;/td&gt;
&lt;td&gt;&lt;input name="h016" value="0" onChange="EvalAny(this, 16)" size=8&gt;&lt;/input&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/form&gt;
&lt;/center&gt;

 

&lt;!-- Script Size:  2.50 KB --&gt;</textarea><br><font FACE="helvetica,arial,geneva"></font></td></tr>

</table>
</form>
</FONT>
</CENTER>


</center>
</body></html>
